package com.cskaoyan.mall.payment.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeCloseModel;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.cskaoyan.mall.common.constant.ResultCodeEnum;
import com.cskaoyan.mall.common.execption.BusinessException;
import com.cskaoyan.mall.order.dto.OrderInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

@Component
public class AliPayHelper implements PayHelper {
    @Autowired
    AlipayClient alipayClient;

    //同步回调地址
    @Value("${alipay.config.return_order_url}")
    String returnPaymentUrl;

    //异步回调地址
    @Value("${alipay.config.notify_payment_url}")
    String notifyPaymentUrl;

    public String createClient(OrderInfoDTO orderInfoDTO ) throws AlipayApiException {
        //然后调用阿里云api补全这里即可
        AlipayTradePagePayRequest alipayTradePagePayRequest = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel alipayTradePagePayModel = new AlipayTradePagePayModel();

        alipayTradePagePayModel.setOutTradeNo(orderInfoDTO.getOutTradeNo());
        alipayTradePagePayModel.setTotalAmount(String.valueOf(orderInfoDTO.getTotalAmount()));
        alipayTradePagePayModel.setSubject(orderInfoDTO.getTradeBody());
        alipayTradePagePayModel.setProductCode("FAST_INSTANT_TRADE_PAY");

        // 创建SimpleDateFormat对象，并指定输出的日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 使用format方法将Date对象转换为String，并按照指定格式输出
        String expireTime = sdf.format(orderInfoDTO.getExpireTime());

        alipayTradePagePayModel.setTimeExpire(expireTime);

        //然后这个地方还要加重定向相关的内容
        //设置同步回调
        alipayTradePagePayRequest.setReturnUrl(returnPaymentUrl);

        //设置异步回调
        alipayTradePagePayRequest.setNotifyUrl(notifyPaymentUrl);


        alipayTradePagePayRequest.setBizModel(alipayTradePagePayModel);

        AlipayTradePagePayResponse alipayTradePagePayResponse = alipayClient.pageExecute(alipayTradePagePayRequest, "POST");
        if (alipayTradePagePayResponse.isSuccess()) {
            return alipayTradePagePayResponse.getBody();
        }else {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }

    }

    //根据订单id获取支付状态
    public String getTradeStatus(String outTradeNo) throws AlipayApiException {
        // 1. 构建请求参数
        AlipayTradeQueryRequest queryRequest = new AlipayTradeQueryRequest();


        // 2. 构建请求体对象
        AlipayTradeQueryModel queryModel = new AlipayTradeQueryModel();
        queryModel.setOutTradeNo(outTradeNo);                                  // 传入交易流水号


        // 3. 把请求体对象放入请求对象中
        queryRequest.setBizModel(queryModel);

        // 4. 调用API，发起请求
        AlipayTradeQueryResponse queryResponse = alipayClient.execute(queryRequest);

        return queryResponse.getTradeStatus();
    }

    //关闭支付宝对应的订单
    public boolean closeAlipay(String outTradeNo) throws AlipayApiException {
        // 1. 构建关闭交易的请求
        AlipayTradeCloseRequest closeRequest = new AlipayTradeCloseRequest();

        // 2. 创建请求体对象
        AlipayTradeCloseModel closeModel = new AlipayTradeCloseModel();

        // 填充请求参数
        closeModel.setOutTradeNo(outTradeNo);


        // 3. 把请求体对象放入请求对象中
        closeRequest.setBizModel(closeModel);


        // 4. 发起请求
        AlipayTradeCloseResponse closeResponse = alipayClient.execute(closeRequest);

        // 5. 获取结果
        boolean success = closeResponse.isSuccess();

        return success;

    }
}
